# - extract paths, libraries from xxx-config scripts
#
# This defines a PACKAGE_SCRIPT macro for feeding flags to pkg-config
# and *-config scripts. These scripts typically accept --cflags and 
# --libs options. Example:
#
#  PACKAGE_SCRIPT( libpng --cflags PNG_CFLAGS --libs PNG_LFLAGS )
#
# This will ask pkg-config about the libpng package, and if that fails,
# it will try a libpng-config script. It will store the result of
# `pkg-config libpng --cflags' or `libpng-config --cflags' in
# PNG_CFLAGS and that of --libs in PNG_LIBRARIES. If the macro finds
# no script, then it empties those variables.
#
# This also defines a PACKAGE_PATHS macro for extracting
# semicolon-separated search paths for include files and libraries.
# Example:
#
#  PACKAGE_PATHS( libpng PNG_INCLUDE_SP PNG_LIB_SP )
#
# This extracts from -I and -L flags in --cflags and --libs.
# Again, if there is no script, the macro empties the variables.

# Modules/MacroPackageScript.cmake --Kernigh, January 2007
# This module is in the public domain and has no copyright.

FIND_PROGRAM( PKGCONFIG_EXECUTABLE pkg-config )
MARK_AS_ADVANCED( PKGCONFIG_EXECUTABLE )

# Create our own PKG_CONFIG_PATH and place it in the cache so that
# the user can modify it.
IF( PKGCONFIG_EXECUTABLE AND NOT DEFINED PKG_CONFIG_PATH )
  SET( PKG_CONFIG_PATH )
  IF( "" STREQUAL "$ENV{PKG_CONFIG_PATH}" )
    # no PKG_CONFIG_PATH in environment
  ELSE( "" STREQUAL "$ENV{PKG_CONFIG_PATH}" )
    LIST( APPEND PKG_CONFIG_PATH "$ENV{PKG_CONFIG_PATH}" )
  ENDIF( "" STREQUAL "$ENV{PKG_CONFIG_PATH}" )

  FOREACH( place
    ${CMAKE_INSTALL_PREFIX}/lib/pkgconfig # Object Necromancer
    /usr/local/lib/qt4                    # OpenBSD
  )
    IF( IS_DIRECTORY ${place} )
      LIST( APPEND PKG_CONFIG_PATH ${place} )
    ENDIF( IS_DIRECTORY ${place} )
  ENDFOREACH( place )

  # Windows pkg-config uses semicolons, but Unix pkg-config uses colons
  IF( UNIX AND NOT WIN32 )
    STRING( REGEX REPLACE \; : PKG_CONFIG_PATH "${PKG_CONFIG_PATH}" )
  ENDIF( UNIX AND NOT WIN32 )

  SET( PKG_CONFIG_PATH "${PKG_CONFIG_PATH}" CACHE STRING
       "A PKG_CONFIG_PATH environment variable for pkg-config." )
ENDIF( PKGCONFIG_EXECUTABLE AND NOT DEFINED PKG_CONFIG_PATH )
SET( ENV{PKG_CONFIG_PATH} "${PKG_CONFIG_PATH}" ) # export

MACRO( PACKAGE_SCRIPT package )
  SET( _ps_pkg_config NO ) # use pkg-config libpng ?
  SET( _ps_own_config NO ) # use libpng-config ?
  SET( _ps_have_flag NO )  # loop iteration has a flag ?

  # first try pkg-config ${package}
  IF( PKGCONFIG_EXECUTABLE )
    EXECUTE_PROCESS( COMMAND ${PKGCONFIG_EXECUTABLE}
                             --exists ${package}
                     RESULT_VARIABLE _ps_pkg_exists_result )
    IF( _ps_pkg_exists_result STREQUAL 0 )
      SET( _ps_pkg_config YES )
    ENDIF( _ps_pkg_exists_result STREQUAL 0 )
  ENDIF( PKGCONFIG_EXECUTABLE )

  # second try ${package}-config
  IF( NOT _ps_pkg_config )
    EXECUTE_PROCESS( COMMAND which ${package}-config
                     RESULT_VARIABLE _ps_own_exists_result
                     OUTPUT_VARIABLE _ps_dev_null )
    IF( _ps_own_exists_result STREQUAL 0 )
      SET( _ps_own_config YES )
    ENDIF( _ps_own_exists_result STREQUAL 0 )
  ENDIF( NOT _ps_pkg_config )

  # feed the parameters to the script
  FOREACH( param ${ARGN} )
    IF( _ps_have_flag )
      IF( _ps_pkg_config )
        EXECUTE_PROCESS( COMMAND ${PKGCONFIG_EXECUTABLE}
                                 ${package} ${_ps_flag}
                         OUTPUT_VARIABLE ${param} )
      ELSE( _ps_pkg_config )
        IF( _ps_own_config )
          EXECUTE_PROCESS( COMMAND ${package}-config ${_ps_flag}
                           OUTPUT_VARIABLE ${param} )
        ELSE( _ps_own_config )
          SET( ${param} ) # empty the variable
        ENDIF( _ps_own_config )
      ENDIF( _ps_pkg_config )
      SET( _ps_have_flag NO )
    ELSE( _ps_have_flag )
      SET( _ps_flag ${param} )
      SET( _ps_have_flag YES )
    ENDIF( _ps_have_flag )
  ENDFOREACH( param )

ENDMACRO( PACKAGE_SCRIPT )

MACRO( PACKAGE_PATHS package include_var lib_var )
  SET( _pp_include )
  SET( _pp_lib )

  PACKAGE_SCRIPT( ${package} --cflags _pp_cflags
                             --libs _pp_lflags )

  # FOREACH wants semicolons not whitespace
  STRING( REGEX REPLACE "[ \n\r\t]+" \; _pp_cflags "${_pp_cflags}" )
  STRING( REGEX REPLACE "[ \n\r\t]+" \; _pp_lflags "${_pp_lflags}" )

  # process -I flags, separating -I switches from arguments
  FOREACH( flag ${_pp_cflags} )
    STRING( LENGTH "${flag}" _pp_flag_len )
    IF( _pp_flag_len GREATER 2 )
      STRING( SUBSTRING "${flag}" 0 2 _pp_switch )
      IF( _pp_switch STREQUAL "-I" )
        MATH( EXPR _pp_arg_len "${_pp_flag_len} - 2" )
        STRING( SUBSTRING "${flag}" 2 ${_pp_arg_len} _pp_arg )
        SET( _pp_include ${_pp_include} ${_pp_arg} )
      ENDIF( _pp_switch STREQUAL "-I" )
    ENDIF( _pp_flag_len GREATER 2 )
  ENDFOREACH( flag )

  # process -L flags
  FOREACH( flag ${_pp_lflags} )
    STRING( LENGTH "${flag}" _pp_flag_len )
    IF( _pp_flag_len GREATER 2 )
      STRING( SUBSTRING "${flag}" 0 2 _pp_switch )
      IF( _pp_switch STREQUAL "-L" )
        MATH( EXPR _pp_arg_len "${_pp_flag_len} - 2" )
        STRING( SUBSTRING "${flag}" 2 ${_pp_arg_len} _pp_arg )
        SET( _pp_lib ${_pp_lib} ${_pp_arg} )
      ENDIF( _pp_switch STREQUAL "-L" )
    ENDIF( _pp_flag_len GREATER 2 )
  ENDFOREACH( flag )

  SET( ${include_var} ${_pp_include} )
  SET( ${lib_var} ${_pp_lib} )
ENDMACRO( PACKAGE_PATHS )
